package com.ruoyi.crm.controller;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.enums.erp.CrmOrderProgessEnum;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.taobao.api.ApiException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.crm.domain.CrmOrder;
import com.ruoyi.crm.service.ICrmOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商机Controller
 *
 * @author yanwenliang
 * @date 2024-10-23
 */
@RestController
@RequestMapping("/crm/order")
public class CrmOrderController extends BaseController
{
    @Autowired
    private ICrmOrderService crmOrderService;

    /**
     * 查询商机列表
     */
    @PreAuthorize("@ss.hasPermi('crm:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(CrmOrder crmOrder)
    {
        startPage();
        List<CrmOrder> list = crmOrderService.selectCrmOrderList(crmOrder);
        return getDataTable(list);
    }

    /**
     * 导出商机列表
     */
    @PreAuthorize("@ss.hasPermi('crm:order:export')")
    @Log(title = "商机", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmOrder crmOrder)
    {
        List<CrmOrder> list = crmOrderService.selectCrmOrderList(crmOrder);
        ExcelUtil<CrmOrder> util = new ExcelUtil<CrmOrder>(CrmOrder.class);
        util.exportExcel(response, list, "商机数据");
    }

    /**
     * 获取商机详细信息
     */
    @PreAuthorize("@ss.hasPermi('crm:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") String orderId)
    {
        return success(crmOrderService.selectCrmOrderByOrderId(orderId));
    }

    /**
     * 新增商机
     */
    @PreAuthorize("@ss.hasPermi('crm:order:add')")
    @Log(title = "商机", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmOrder crmOrder) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException, ApiException {
        return toAjax(crmOrderService.insertCrmOrder(crmOrder));
    }

    /**
     * 修改商机
     */
    @PreAuthorize("@ss.hasPermi('crm:order:edit')")
    @Log(title = "商机", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmOrder crmOrder)
    {
        return toAjax(crmOrderService.updateCrmOrder(crmOrder));
    }

    /**
     * 删除商机
     */
    @PreAuthorize("@ss.hasPermi('crm:order:remove')")
    @Log(title = "商机", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable String[] orderIds) {
        return toAjax(crmOrderService.deleteCrmOrderByOrderIds(orderIds));
    }

    @PostMapping("getCrmOrderAllNoPage")
    public AjaxResult getCrmOrderAllNoPage(@RequestBody CrmOrder crmOrder){

        return AjaxResult.success(crmOrderService.list(new LambdaQueryWrapper<CrmOrder>()
                .eq(CrmOrder::getStatus, "1")
                .eq(StringUtils.isNotEmpty(crmOrder.getOrderType()), CrmOrder::getOrderType, crmOrder.getOrderType())
                .eq(StringUtils.isNotEmpty(crmOrder.getCustomerId()), CrmOrder::getCustomerId, crmOrder.getCustomerId())
                .orderByDesc(CrmOrder::getCreateTime)));
    }

    /**
     * 获取订单信息
     * @param crmOrder
     * @return
     */
    @PostMapping("getOrderOne")
    public AjaxResult getOrderOne(@RequestBody CrmOrder crmOrder){
        crmOrder.setUserId(SecurityUtils.getUserId());
        CrmOrder order = crmOrderService.getOrderOne(crmOrder);
        return AjaxResult.success(order);
    }


    @PostMapping("/getOrderListAll")
    public AjaxResult getOrderListAll(@RequestBody CrmOrder crmOrder) {
        return AjaxResult.success(crmOrderService.list(new LambdaQueryWrapper<CrmOrder>()
                .eq(StringUtils.isNotEmpty(crmOrder.getCustomerId()), CrmOrder::getCustomerId, crmOrder.getCustomerId())
                .in(CrmOrder::getProgess, CrmOrderProgessEnum.BIDDING.getCode(), CrmOrderProgessEnum.DEAL_CLOSED.getCode()
                        , CrmOrderProgessEnum.INSPECTION_COMPLETED.getCode(), CrmOrderProgessEnum.SETTLEMENT_COMPLETED.getCode())
                .orderByDesc(CrmOrder::getCreateTime)));
    }

    @PostMapping("/setRenewal")
    public AjaxResult setRenewal(@RequestBody CrmOrder crmOrder){
        return AjaxResult.success(crmOrderService.setRenewal(crmOrder));
    }
}
